<?php
class Asd_Controller extends Zend_Controller_Action {
    
    public function init() {
        parent::init();
        
        if (Zend_Auth::getInstance()->hasIdentity()) {
            $db = Zend_Db_Table::getDefaultAdapter();
            $select = $db->select()
                    ->from('messages', array(new Zend_Db_Expr('COUNT(user_id) as count')))
                    ->where('user_id = ?', Zend_Auth::getInstance()->getIdentity()->user_id)
                    ->order('user_id');
            $result = $db->fetchAll($select);
            $this->view->messageCount = $result[0]['count'];
        }
    }
}